<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入名称" clearable
                          @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="日期" prop="date">
                <el-date-picker clearable v-model="queryParams.date" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择开始时间"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择结束时间"></el-date-picker>
            </el-form-item>
<!--            <el-form-item label="类型" prop="type">-->
<!--                <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>-->
<!--                    <el-option v-for="dict in dict.type.go_score_exam_type_dict" :key="dict.value" :label="dict.label" :value="dict.value"/>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="状态" prop="status">-->
<!--                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>-->
<!--                    <el-option v-for="dict in dict.type.go_score_exam_status_dict" :key="dict.value" :label="dict.label" :value="dict.value"/>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                           v-hasPermi="['go_score:go_score_exam:add']">新增
                </el-button>
            </el-col>
<!--            <el-col :span="1.5">-->
<!--                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"-->
<!--                           v-hasPermi="['go_score:go_score_exam:edit']">修改-->
<!--                </el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                           @click="handleDelete" v-hasPermi="['go_score:go_score_exam:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                           v-hasPermi="['go_score:go_score_exam:export']">导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="go_score_examList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="名称" align="center" prop="name">
                <template slot-scope="scope">
                    <el-button type="text" @click="toGoScoreExamScore(scope.row)">{{scope.row.name}}</el-button>
                </template>
            </el-table-column>
            <el-table-column label="科室" align="center" prop="deptName"/>
            <el-table-column label="日期" align="center" prop="date">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="考试时间" align="center" prop="startTime">
                <template slot-scope="scope">
                    {{scope.row.startTime}} ~ {{scope.row.endTime}}
                </template>
            </el-table-column>
<!--            <el-table-column label="类型" align="center" prop="type">-->
<!--                <template slot-scope="scope">-->
<!--                    <dict-tag :options="dict.type.go_score_exam_type_dict" :value="scope.row.type"/>-->
<!--                </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column label="状态" align="center" prop="status">-->
<!--                <template slot-scope="scope">-->
<!--                    <dict-tag :options="dict.type.go_score_exam_status_dict" :value="scope.row.status"/>-->
<!--                </template>-->
<!--            </el-table-column>-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['go_score:go_score_exam:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['go_score:go_score_exam:remove']">删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-document" @click="handleExportRecordScore(scope.row)" v-hasPermi="['go_score:go_score_ex_exam_mark']">成绩单</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

        <!-- 添加或修改妇产科考试对话框 -->
        <el-drawer :title="title" :visible.sync="open" size="60%" append-to-body destroy-on-close>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="10">
                    <el-col :span="6">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入名称" clearable style="width: 200px"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="科室" prop="deptId">
                            <treeselect v-model="form.deptId" :options="deptOptions" :props="defaultProps" :defaultExpandLevel="Infinity"
                                        :show-count="true" clearable placeholder="请选择科室" style="width: 320px"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="6">
                        <el-form-item label="日期" prop="date">
                            <el-date-picker clearable v-model="form.date" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期" style="width: 200px"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="考试时间" prop="timeRange">
                            <el-time-picker clearable is-range v-model="form.timeRange" value-format="HH:mm:ss" range-separator="~"
                                            start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围" style="width: 320px"/>
                        </el-form-item>
                    </el-col>
                </el-row>

<!--                <el-row :gutter="10">-->
<!--                    <el-col :span="6">-->
<!--                        <el-form-item label="类型" prop="type">-->
<!--                            <el-select v-model="form.type" placeholder="请选择类型">-->
<!--                                <el-option v-for="dict in dict.type.go_score_exam_type_dict" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>-->
<!--                            </el-select>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                    <el-col :span="6">-->
<!--                        <el-form-item label="状态" prop="status">-->
<!--                            <el-radio-group v-model="form.status">-->
<!--                                <el-radio v-for="dict in dict.type.go_score_exam_status_dict" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>-->
<!--                            </el-radio-group>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                </el-row>-->
            </el-form>
            <el-divider content-position="center">考场信息</el-divider>
            <div class="form_place_info">
                <el-tabs v-model="form_place_action" type="border-card" editable @edit="handleFormPlaceTabsEdit">
                    <el-tab-pane v-for="(place,index) in goScoreExamPlaceList" :label="'考场 '+(index+1)" :name="index+''">
                        <GO_SCORE_EXAM_PLACE :place="place" :teacher-list="teacherList" :student-list="studentList" :student-loading="student_loading"
                                             :place-list="placeList" :skill-list="skillList" :skill-loading="skill_loading"/>
                    </el-tab-pane>
                </el-tabs>
            </div>
            <div style="width: 100%;text-align: center;padding: 20px">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-drawer>
    </div>
</template>

<script>
import {
    listGo_score_exam,
    getGo_score_exam,
    delGo_score_exam,
    addGo_score_exam,
    updateGo_score_exam
} from "@/api/go_score/go_score_exam";
import {getGo_score_ex_exam_score,} from "@/api/go_score/go_score_ex_exam_mark";
import {listEx_place} from "@/api/basic/ex_place";
import {listTeacher} from "@/api/system/teacher";
import {listStudent} from "@/api/system/student";
import {deptTreeSelect} from "@/api/system/user";
import {listSkill} from "@/api/score/skill";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import GO_SCORE_EXAM_PLACE from "../go_score_exam_place"
import {isNotNull,copyObj} from "@/utils/zzDouUtils/objUtil";
import {nowDateStr, nowTimeStr} from "@/utils/zzDouUtils/dateUtil";

var searchParam = {pageSize: 1000, pageNum: 1};
export default {
    name: "Go_score_exam",
    components: {Treeselect,GO_SCORE_EXAM_PLACE},
    dicts: ['go_score_exam_type_dict', 'go_score_exam_status_dict'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 妇产科考试表格数据
            go_score_examList: [],
            teacherList: [],
            placeList: [],
            deptOptions: [],   // 科室树选项
            defaultProps: {
                children: "children",
                label: "label"
            },
            studentList: [],
            student_loading: false,
            skillList: [],
            skill_loading: false,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                date: null,
                startTime: null,
                endTime: null,
                type: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 妇产科考试-考场表格数据
            goScoreExamPlaceList: [],
            form_place_action: 0,
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "名称不能为空", trigger: "blur" }
                ],
                date: [
                    { required: true, message: "请选择日期", trigger: "blur" }
                ],
                deptId: [
                    { required: true, message: "请选择科室", trigger: "blur" }
                ],
                timeRange: [
                    { type: 'array',required: true, message: "请选择时间", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
        this.getDeptTree();
        this.getExPlaceList();
        this.getTeacherList();
    },
    watch: {
        'form.deptId': {//科室修改后，查询该科室下的学员、技能
            handler(newVal, oldVal) {
                //学员列表
                this.getStudentList(newVal);
                //技能列表
                this.getSkillList(newVal);
            },
            // 开启深度监听
            deep: true
        }
    },
    methods: {
        /** 查询技能管理列表 */
        getSkillList(deptId) {
            if (isNotNull(deptId)) {
                this.skill_loading = true;
                let param = copyObj(searchParam);
                param['deptId'] = deptId;
                listSkill(param).then(response => {
                    this.$set(this,'skillList', response.rows);
                    this.skill_loading = false;
                });
            }else {
                this.$set(this,'skillList', []);
            }
        },
        /** 查询教师列表 */
        getTeacherList() {
            listTeacher(searchParam).then(response => {
                this.teacherList = response.rows;
            });
        },
        /** 查询学生列表 */
        getStudentList(deptId) {
            if (isNotNull(deptId)) {
                this.student_loading = true;
                let param = copyObj(searchParam);
                param['deptId'] = deptId;
                listStudent(param).then(response => {
                        this.$set(this, 'studentList', response.rows.map(item => {
                            return {
                                key: item.userId,
                                label: `<span style="margin-right: 10px;color: #191c24">${item.nickName}</span><span style="font-size: 10px;color: #4AB7BD">(${item.dept.deptName})</span>`
                            }
                        }));
                        this.student_loading = false;
                    }
                );
            } else {
                this.$set(this, 'studentList', []);
            }
        },
        /** 查询考站表列表 */
        getExPlaceList() {
            listEx_place(searchParam).then(response => {
                this.placeList = response.rows;
            });
        },
        /** 查询科室下拉树结构 */
        getDeptTree() {
            deptTreeSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        /** 查询妇产科考试列表 */
        getList() {
            this.loading = true;
            listGo_score_exam(this.queryParams).then(response => {
                this.go_score_examList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                date: nowDateStr(),
                startTime: null,
                endTime: null,
                deptId: null,
                type: null,
                status: null,
                createBy: null,
                createTime: null,
                timeRange: [nowTimeStr(),nowTimeStr()]
            };
            this.form_place_action = '0';
            this.goScoreExamPlaceList = [];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.handleAddGoScoreExamPlace();
            this.open = true;
            this.title = "添加妇产科考试";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            getGo_score_exam(row.id).then(response=>{
                this.form = response.data;
                this.goScoreExamPlaceList = response.data.goScoreExamPlaceList;
                this.open = true;
                this.title = "修改妇产科考试";
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.goScoreExamPlaceList = this.goScoreExamPlaceList;
                    if (this.form.id != null) {
                        updateGo_score_exam(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addGo_score_exam(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除妇产科考试编号为"' + ids + '"的数据项？').then(function () {
                return delGo_score_exam(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 妇产科考试-考场添加按钮操作 */
        handleAddGoScoreExamPlace() {
            let obj = {
                placeId:"",
                teacherIds: "",
                skillId: "",
                sheetIds: "",
                studentIdList: [],
            }
            this.$set(this.goScoreExamPlaceList,this.goScoreExamPlaceList.length,obj);
            this.$set(this,'form_place_action',(this.goScoreExamPlaceList.length-1)+'')
        },
        handleFormPlaceTabsEdit: function (targetName, action) {
            if (action === 'add') {
                this.handleAddGoScoreExamPlace();
            } else if (action === 'remove') {
                this.handleDeleteGoScoreExamPlace(parseInt(targetName));
            }
        },
        /** 妇产科考试-考场删除按钮操作 */
        handleDeleteGoScoreExamPlace(index) {
            let form_action = parseInt(this.form_place_action);
            let action = this.form_place_action;
            if (form_action === index && index === 0){//删除第一个
                if (this.goScoreExamPlaceList.length > 1){
                    action = '0';
                }
            }
            if (form_action === index && index === this.goScoreExamPlaceList.length-1){//删除最后一个
                if (this.goScoreExamPlaceList.length > 1){
                    action = index-1 + '';
                }
            }
            this.$delete(this.goScoreExamPlaceList,index);
            this.form_place_action = action;
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('go_score/go_score_exam/export', {
                ...this.queryParams
            }, `go_score_exam_${new Date().getTime()}.xlsx`)
        },
        /** 导出成绩单 */
        handleExportRecordScore(row){
            this.download('go_score/go_score_ex_exam_mark/export', {
                examId: row.id
            }, `${row.name}成绩单_${nowDateStr()}.xlsx`)
        },
        /** 跳转打分页面 */
        toGoScoreExamScore(row){
            this.$tab.openPage(row.name, '/go_score/go_score_ex_exam_mark/index.vue/'+ row.id, {});
        },
        /** 跳转成绩单页面 */
        toGoScoreExamScoreRecord(row){
            this.$tab.openPage(row.name, '/go_score/go_score_ex_exam_mark/record_index.vue/'+ row.id, {});
        }
    }
};
</script>
<style scoped>
.form_place_info{/* 考试信息盒子 */
    width: 97%;
    margin: 0 auto;
    /*box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)*/
}

/deep/.el-tabs__new-tab {
    float: right;
    border: 1px solid #409EFF;
    height: 22px;
    width: 22px;
    line-height: 22px;
    margin: 10px;
    border-radius: 3px;
    text-align: center;
    font-size: 15px;
    color: #409EFF;
    cursor: pointer;
    -webkit-transition: all .15s;
    transition: all .15s;
}
</style>
